package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author zhangbaiyan
 * @date 2024-09-30 21:46
 * @description:
 * @version:
 */
@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 本地存储
     * @param name
     * @param age
     * @param image
     * @return result
     * @throws IOException
     */
//    @PostMapping
//    public Result update(String name , Integer age, MultipartFile image) throws IOException {
//        log.info("name:{},age:{},image:{}",name,age,image);
//
//        //将文件存储到服务器的磁盘目录中 E:\images
//        image.transferTo(new File("E:\\images\\"+
//                UUID.randomUUID().toString().replace("-", "")
//                +image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."))));
//        return Result.success();
//    }

    @PostMapping
    public Result update(MultipartFile image) throws IOException, ClientException {
        log.info("image:{}",image.getOriginalFilename());
        String url = aliOSSUtils.upload(image);
        log.info("文件上传成功，访问路径为：{}",url);
        return Result.success(url);
    }
}
